import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';
import { Api } from '/@/api/url'

export const getExportUrl = Api.exportUserXls;
export const getImportUrl = Api.importRoleExcel;

export const pageRole = (params) => defHttp.get({url: Api.pageRole, params});

export const deleteRole = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteRole, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteRole = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.batchDeleteRole, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const saveOrUpdateRole = (params, isUpdate) => {
  let url = isUpdate ? Api.updateRole : Api.saveRole;
  return defHttp.post({url: url, params});
}

export const isRoleExist = (params) =>
  defHttp.get({url: Api.isRoleExist, params}, {isTransformResponse: false});


 export const listDeptTreeForRole = () =>
 defHttp.get({url: Api.listDeptTreeForRole});


export const listDeptInRole = (params) => defHttp.get({url: Api.listDeptInRole, params});

export const queryRolePermission = (params) =>
  defHttp.get({url: Api.queryRolePermission, params});

export const saveRolePermission = (params) =>
  defHttp.post({url: Api.saveRolePermission, params});

export const updateDataScope = (params) => defHttp.post({url: Api.updateRoleDataScope, params});

export const queryDataRule = (params) =>
  defHttp.get({url: Api.queryRoleDataRule, params: {
    permissionId: params.functionId,
    roleId: params.roleId
  }}, {isTransformResponse: true});

export const saveDataRule = (params) => defHttp.post({url: Api.saveRoleDataRule,params});

export const getParentDesignList = () => defHttp.get({url: Api.getParentDesignList});

export const getRoleDegisnList = (params) => defHttp.get({url: Api.getRoleDegisnList,params});
export const saveRoleDesign = (params) => defHttp.post({url: Api.saveRoleDesign,params});

export const userList = (params) => defHttp.get({url: Api.listRoleUser, params});

export const deleteUserRole = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteRoleUser, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteUserRole = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.batchDeleteRoleUser, params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const addUserRole = (params, handleSuccess) => {
  return defHttp.post({url: Api.addRoleUser, params}).then(() => {
    handleSuccess();
  });
}

export const listApiResource = (params) => {
  return defHttp.get({url: Api.listApiResource, params});
}

export const saveApiResource = (params) => {
  return defHttp.post({url: Api.saveApiResource, params});
}